package com.chinacache.maint.webssh.action.ssh;

import org.springframework.beans.factory.annotation.Required;

import com.chinacache.maint.commons.orm.hibernate.Page;
import com.chinacache.maint.commons.util.DateUtils;
import com.chinacache.maint.commons.util.HttpUtil;
import com.chinacache.maint.commons.web.struts2.CRUDActionSupport;
import com.chinacache.maint.webssh.entity.ssh.CommandLog;
import com.chinacache.maint.webssh.service.ssh.CommandLogManager;
import com.chinacache.maint.webssh.service.ssh.DeviceManager;

@SuppressWarnings("serial")
public class CommandLogAction extends CRUDActionSupport<CommandLog> {
	private CommandLogManager manager;
	private DeviceManager deviceManager;
	private CommandLog entity;
	private Long id;
	private Page<CommandLog> page = new Page<CommandLog>();
	private String condition;

	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	@Required
	public void setCommandLogManager(CommandLogManager commandLogManager) {
		manager = commandLogManager;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Override
	public String delete() throws Exception {
		return null;
	}

	@Override
	public CommandLog getModel() {
		return entity;
	}

	@Override
	public Page<CommandLog> getPage() {
		return page;
	}

	@Override
	public String list() throws Exception {
		// page = manager.getAllCommandLog(page);
		page = manager.getAllCommandLogByCondition(page, condition);
		return SUCCESS;
	}

	@Override
	public void prepare() throws Exception {
		if (id != null) {
			entity = manager.getCommandLog(id);
		} else {
			entity = new CommandLog();
		}
	}

	@Override
	public String save() throws Exception {
		String commandLog = HttpUtil.getHttpServletRequest().getParameter(
				"commandLog");
		if (commandLog.length() > 0) {
			String devIdString = HttpUtil.getHttpServletRequest().getParameter(
					"devId");
			this.entity.setCommandLog(commandLog);
			this.entity.setDevice(deviceManager.getDevice(devIdString));
			this.entity.setCommandType(new Integer("0"));
			this.entity.setUserName((String) HttpUtil
					.getFromSession("userName"));
			this.entity.setTime(DateUtils.getCurrTime());
			manager.saveCommandLog(this.entity);
		}
		return null;
	}

	public DeviceManager getDeviceManager() {
		return deviceManager;
	}

	public void setDeviceManager(DeviceManager deviceManager) {
		this.deviceManager = deviceManager;
	}

}
